/**
 * Created by STICKYQU on 2016/7/7.
 */

function getParameterByName(name, url) {
    if (!url) url = window.location.href;
    name = name.replace(/[\[\]]/g, "\\$&");
    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
        results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/\+/g, " "));
}
function getallconfig(){
    $.ajax({
        url: "/cgi/getallconfig.php",
        type: "post",
        dataType:"json",
        data: {

        },
        success: function(data) {
            //{code:0,skey:"skey",user:"" }
            if (data.code == 0) {
                var datas=data.data;
                var strhtml="";
                for(var i=0;i<datas.length;i++){
                   var htmlitems= '<tr>' +
                    '<td>' +
                    '<label class="tbd-label">' +
                    '<span class="tbd-checkbox"></span>' +
                    '</label>' +
                    '</td><td>' +
                    '<a href="/viewconfig.html?id='+datas[i].id+'" class="message-cnt"  >' +
                    '<i class="tbdi tbdi-mail"></i>'+datas[i].appname+' </a>' +
                    '</td>' +
                    ' <td>'+datas[i].description+'</td>' +
                    ' <td>'+datas[i].type+'</td>' +
                    ' </tr>';
                    strhtml+=htmlitems;
                }
                $("#configlist tbody").html(strhtml);
            }
            else {
                console.error("获取配置信息失败!");
            }
        },
        error:function(respone){
            if(console&& console.error)
                console.error(respone.responseText);
        }
    });
}

function getconfig(){
    var id=getParameterByName('id');
    $.ajax({
        url: "/cgi/getconfig.php",
        type: "post",
        dataType:"json",
        data: {
            id:id
        },
        success: function(data) {
            //{code:0,skey:"skey",user:"" }
            if (data.code == 0) {
                var configdata=data.data;
                    /*<div>系统名称:<span id="appname"></span></div>
                    <div>系统描述:<span id="description"></span></div>
                    <div>系统域名:<span id="domains"></span></div>
                    <div>支持用户:<span id="type"></span></div>
                    <div>系统key:<span id="appkey"></span></div>
                    <div>系统秘钥:<span id="secret"></span></div>*/
                $("#appname").html(configdata.appname);
                $("#description").html(configdata.description);
                $("#domains").html(configdata.domains);
                $("#type").html(configdata.type);
                $("#appkey").html(configdata.appkey);
                $("#secret").html(configdata.secret);
            }
            else {
                console.error("获取配置信息失败!");
            }
        },
        error:function(respone){
            if(console&& console.error)
                console.error(respone.responseText);
        }
    });
}


function registerconfig(){
    var id=getParameterByName('id');
    $.ajax({
        url: "/cgi/register.php",
        type: "post",
        dataType:"json",
        data: {
            appname:$("#appname").val(),
            description:$("#description").val(),
            domains:$("#domains").val(),
            usertypes:$("#type").val()
        },
        success: function(data) {
            //{code:0,skey:"skey",user:"" }
            if (data.code == 0) {
                location.href="/viewconfig.html?id="+data.data;
            }
            else {
                console.error("获取配置信息失败!");
            }
        },
        error:function(respone){
            if(console&& console.error)
                console.error(respone.responseText);
        }
    });
}